package com.acwing.partition20;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2022/6/3 10:32
 */
public class AC1927自动补全 {

    private static final int N = 30010;
    private static Map<String, Integer> map = new HashMap<>(N);
    private static String[] dict = new String[N];
    private static int n, q;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt(); q = nextInt();
        for (int i = 1; i <= n; i++) {
            dict[i] = next();
            map.put(dict[i], i);
        }
        Arrays.sort(dict, 1, n + 1);
        while (q-- > 0) {
            int k = nextInt(), l = 1, r = n + 1;
            String s = next();
            //找到第一个以当前字符串为前缀的串
            while (l < r) {
                int mid = l + r >> 1;
                if (dict[mid].compareTo(s) >= 0) r = mid;
                else l = mid + 1;
            }
            if (l + k - 1 > n || !dict[l + k - 1].startsWith(s)) writer.println(-1);
            else writer.println(map.get(dict[l + k - 1]));
        }
        writer.flush();
    }

    private static String next() throws IOException {
        tokenizer.nextToken();
        return tokenizer.sval;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
